<%@page import="service.GoodsService"%>
<%@page import="util.Util"%>
<%@page import="entities.CartEntity"%>
<%@page import="service.CartService"%>
<%@page import="entities.GoodsEntity"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%	
	//设置页面不缓存
	response.setHeader("Pragma", "No-cache");
	response.setHeader("Cache-Control", "no-cache");
	response.setDateHeader("Expires", 0);
	
	String userName = (String) session.getAttribute("userName");
	List<CartEntity> cartList = new ArrayList<CartEntity>();
	if (userName == null) {
		response.sendRedirect("logReg.jsp?action=login");
	} else {
		//获得购物车数据
		cartList = CartService.selectAll(userName);
	}
%>
<!DOCTYPE HTML>
<html>
<head>
<title>购物车-<%=userName%></title>

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/cart.css">
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/GlobalFunction.js" type="text/javascript"></script>
</head>

<body>
	<jsp:include page="header.jsp"></jsp:include>
	<div style="height: 33px;" id ="userName"><%=userName %></div>
	<div class="center">
		<div class="body">
			<div class="cart">
				<div style="width:965px;display:inline-block;">
					<span
						style="float: left;font-size:22px;margin-top:20px;color: green;"><%=userName%>的购物车</span>
					<div class="step" style="float:right;margin:20px 0 0;">
						<img src="images/cart_step1_v1.jpg">
					</div>
				</div>
				<%
					if (cartList.size() != 0) {
				%>
				<div class="cart_products">
					<table width="100%">
						<tbody>
							<tr>
								<th width="170">产品</th>
								<th></th>
								<th width="120">数量</th>
								<th width="120">价格</th>
								<th width="180">总价</th>
								<th width="65">操作</th>
							</tr>
							<%
								double totalPrice = 0d;
								for (CartEntity cart : cartList) {
								totalPrice += cart.getGoodsPrice() * cart.getItemCount();
							%>
							<tr class="cart_item" id="<%=cart.getGoodsId()%>">
									<td class="product_box" width="120"><div class="pic">
											<a href="goodsDetail.jsp?goodsId=<%=cart.getGoodsId()%>"
												target="_blank"> <img width="110" height="70"
												src="<%=cart.getGoodsImg()%>"> </a>
										</div>
									</td>
								<td class="name">
									<div style="width:259px;position: relative;">
										<a href="goodsDetail.jsp?goodsId=<%=cart.getGoodsId()%>"
											target="_blank"> <%=cart.getGoodsName()%> </a> <a
											class="sellerName"
											href="menu.jsp?sellerId=<%=cart.getSellerId()%>"
											target="_blank">【<%=cart.getSellerName()%>】</a>
										<div class="itemTime"><%=cart.getItemTime()%></div>
									</div></td>
								<td class="number_box text_left"><div
										class="buy_number_input">
										<span id="goodsId" style="display: none;"><%=cart.getGoodsId() %></span>
										<span class="decrease_num"></span> <input
											class="item-buy-quantity-input"
											value="<%=cart.getItemCount()%>" /> <span class="increase_num"></span>
									</div>
								</td>
								<td class="price_box">¥<span><%=cart.getGoodsPrice()%></span>
								</td>
								<td class="count_price_box">¥<span><%=cart.getGoodsPrice() * cart.getItemCount()%></span>
								</td>
								<td class="action_box"><a class="item-buy-delete"
									href="javascript:void(0)">删除</a></td>
							</tr>
							<%
								}
							%>
							<tr class="no_border">
								<td colspan="6" class="count"><a class="clean_cart"
									href="javascript:;">清空购物车</a>
									应付总额 ：<span class="totalPrice"> ¥<span><%=totalPrice%></span>
								</span></td>
							</tr>
							<tr class="no_border">
								<td colspan="2" style="text-align:left;"></td>
								<td colspan="4" class="confirm_btn"><a id="go_to_order"
									class="btn_pink_big" href="cartConfirm.jsp">去结算</a> <a
									class="btn_pink_big" href="seller.jsp">继续购物</a>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
				<%
					} else {
				%>

				<div class="cart_empty">
					<span>您的购物车中还没有商品，赶快去挑选吧！</span> <a href="index.jsp">返回商城首页
					</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="seller.jsp">返回商家列表
					</a>
				</div>
				<%
					}
				%>
				<div class="cart_other_products">
					<div class="mall_box" id="featured_star" style="display: block;">
						<div class="title_star">购买了上述商品的用户还购买了&gt;&gt;</div>
						<ul class="product_list" id="product_list_star">
						<%
						List<GoodsEntity> goodsList = new ArrayList<GoodsEntity>();
						goodsList = GoodsService.getOtherCartGoods(userName);
						for(GoodsEntity g:goodsList) {%>
							<li class="cross_sale_deal">
								<div class="pic">
									<a target="_blank" href="goodsDetail.jsp?goodsId=<%=g.getId()%>"><img src="<%=g.getImageUrl() %>" width="160"
										height="160"> </a>
								</div>
								<div class="name">
									<a target="_blank" href="goodsDetail.jsp?goodsId=<%=g.getId()%>"><%=g.getName() %></a>
								</div>
								<div class="price">
									<span>¥<%=g.getPrice() %></span><span class="discount">(销售:<%=g.getSaleCount() %>)</span>
								</div>
								<div class="button" id="addCart" onclick="addCart(<%=g.getId()%>)">加入购物车</div></li>
							<%} %>
						</ul>
					</div>
				</div>
			</div>
		</div>

	</div>
	<jsp:include page="footer.jsp"></jsp:include>
	<script type="text/javascript">
		$(document).ready(function() {
			var userName =$("#userName").html().trim();//用户名
			var totalPrice = $(".totalPrice span");//总价格标签
			$(".increase_num").click(function() {
				update($(this),"increase");
			});
			$(".decrease_num").click(function() {
				var count = $(this).siblings(".item-buy-quantity-input").val();
				if(count==1) return;
				update($(this),"decrease");
			});
			
			$(".item-buy-quantity-input").keyup(function() {
				update($(this),$(this).val());
			});
			
			$(".item-buy-delete").click(function() {
				deleteItem($(this), "deleteOne");
			});
			
			$(".clean_cart").click(function() {
				if(confirm('您确定要清空购物车吗？此操作不能恢复哦~')){
					deleteAll();
				}
			});
			
			//实时修正数量价格函数
			function update(tag,action){
				var number = tag.siblings(".item-buy-quantity-input");//商品数量标签
				var goodsId = tag.parents("tr.cart_item").attr("id");//商品Id
				var goodsPrice = tag.parents("tr.cart_item").children("td.price_box").children("span").html();//商品价格
				var countPrice = tag.parents("tr.cart_item").children("td.count_price_box").children("span");//商品小计标签
				$.ajax({
					type : "POST",
					url : "servlet/CartServlet",
					data : "action=update&goodsId="+goodsId+"&userName=" +userName+"&update="+action,
					success : function(result) {
						var result= result.trim();
						if(result=="failed"){
							alert("亲~~~ ， 修改数量失败了~~"+result*goodsPrice);
						}else{
							number.attr("value",result);//修改数量
							countPrice.html((result*goodsPrice).toFixed(2));//修改小计
							//alert(""+result+"====="+goodsPrice+"==="+totalPrice.html());
							calculate();//修改总计
						}
					}
				});
			}
			
			//删除订单项
			function deleteItem(tag,action){
				//var number = tag.siblings(".item-buy-quantity-input");//商品数量标签
				var goodsId = tag.parents("tr.cart_item").attr("id");//商品Id
				//var goodsPrice = tag.parents("tr.cart_item").children("td.price_box").children("span").html();//商品价格
				//var countPrice = tag.parents("tr.cart_item").children("td.count_price_box").children("span");//商品小计标签
				
				$.ajax({
					type : "POST",
					url : "servlet/CartServlet",
					data : "action=delete&goodsId="+goodsId+"&userName=" +userName,
					success : function(result) {
						var result= result.trim();
						if(result=="success"){
							tag.parents("tr.cart_item").toggle("slow");
							tag.parents("tr.cart_item").remove();
							calculate();//修改总计
						}else {
							alert("亲~~~ ， 删除失败了~~");
						}
					}
				});
			}
			
			//清空购物车
			function deleteAll(){
				//var number = tag.siblings(".item-buy-quantity-input");//商品数量标签
				//var goodsId = tag.parents("tr.cart_item").attr("id");//商品Id
				//var goodsPrice = tag.parents("tr.cart_item").children("td.price_box").children("span").html();//商品价格
				//var countPrice = tag.parents("tr.cart_item").children("td.count_price_box").children("span");//商品小计标签
				
				$.ajax({
					type : "POST",
					url : "servlet/CartServlet",
					data : "action=deleteAll&goodsId=0&userName=" +userName,
					success : function(result) {
						var result= result.trim();
						if(result=="success"){
							$(".cart_products").toggle("slow");
							$(".cart_products").show("slow");
							$(".cart_products").html("<div class='cart_empty'><span>您的购物车中还没有商品，赶快去挑选吧！</span>"
								+" <a href='index.jsp'>点击返回商城首页</a></div>");
						}else {
							alert("亲~~~ ， 清楚购物车失败了~~");
						}
					}
				});
			}
			//计算总价
			function calculate(){
				var total = 0;
				$.each($("td.count_price_box"),function(index,value){
					var price  = Number($(this).children("span").html());
					total+=price;
					//alert(price);
				});
				totalPrice.html(total.toFixed(2));
			}
		});
		
		//加入到购物车
		function addCart(goodsId) {
			var userName = $("#userName").html().trim();
			buy(userName, goodsId);//buy函数写在GlobalFunction中
		};
	</script>
</body>
</html>
